package com.bianmaba.schedulings;

import com.bianmaba.jobs.MethodInvokingJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @program: samples
 * @description:
 * @author: Chenjiabin
 * @create: 2019/10/8 15:22
 **/
@Configuration
public class MethodInvokingJobSchedule {
   // @Bean("useCustomJobDetail")
    public SimpleTriggerFactoryBean useCustomJobDetail() throws NoSuchMethodException, ClassNotFoundException {
        String name = "useCustomJobDetail";
        String group = "group";

        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("description", "使用自定义JobDetail执行指定类及方法的触发器(注：用此方法时MethodInvokingJobDetailFactoryBean必须交由spring管理)-01");

        MethodInvokingJobDetailFactoryBean jobDetailFactory = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactory.setConcurrent(true);
        jobDetailFactory.setName(name);
        jobDetailFactory.setGroup(group);
        jobDetailFactory.setTargetObject(new MethodInvokingJob());
        jobDetailFactory.setTargetMethod("start");
        jobDetailFactory.setArguments(jobDataMap);

        //不使用spring托管时，需要手动执行以下函数以使初始化Factory
        jobDetailFactory.afterPropertiesSet();


        JobDetail jobDetail = jobDetailFactory.getObject();
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(jobDetail);
        trigger.setName("useMethodInvokingJobDetailFactoryBean");
        trigger.setRepeatInterval(1000);
        return trigger;
    }

}
